/**
 * Created by sxwu on 14-2-14.
 */
/*登陆*/

var log = require("../lib/log");
var config = require('../config');
var User = require('../models/viruser');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');


exports.register = function(request, response) {
    var method = request.method || '';
    var data = RUtil.front_data(request);
    data.title = '注册';
    data.uname = '';

    var resdata = RUtil. retdata(request);

    // 登录页面
    if(method.toUpperCase() === "GET") {
        response.render('register', data);
    }

    // 登录验证请求
    else if(method.toUpperCase() === "POST") {
        var viruser_info = RUtil.viruser_info(request);
        viruser_info.username=(request.body.username);
        viruser_info.userpwd=crypto.createHash('md5').update(request.body.userpwd).digest('hex');
        viruser_info.usernick=(request.body.usernick);
        viruser_info.userflag=0; //0:未激活 1：已激活
        viruser_info.usertype=0; //0:普通用户 1：管理员
        viruser_info.email=(request.body.email);
        viruser_info.phonenumber=(request.body.phonenumber);
        viruser_info.companyname=(request.body.companyname);

        User.insert(viruser_info, function(error,results) {
            //log.info("用户信息：" + require('util').inspect(results));
            if((error)!== null)
            {
                // 数据库异常
                resdata.info="注册失败!"+"(错误码："+error.errno+")<BR><BR><a href=\"register.html\">重新注册 </a>";
                resdata.retType="alert-error";
                response.send(resdata);
                return;
            }
            else
            {
                resdata.info="恭喜您：注册成功！<BR><BR><a href=\"login.html\">点击登录 </a>";
                resdata.success=true;
                resdata.retType="alert-info";
                response.send(resdata);
                return;
            }

        });
    }

    };
